import {Component, OnInit} from '@angular/core';
import {CommodityService} from '../commodity.service';
import {Router} from '@angular/router';
import {NzMessageService} from 'ng-zorro-antd/message';

@Component({
  selector: 'app-commodity-catetory',
  templateUrl: './commodity-category.component.html',
  styleUrls: ['./commodity-category.component.scss']
})
export class CommodityCategoryComponent implements OnInit {
  query = {
    // 当前页
    current: 1,
    // 分页大小
    size: 20,
  };
  listOfData = {
    current: 1,
    total: 100,
    size: 20,
    records: []
  };
  checked = false;
  setOfCheckedId = new Set();
  indeterminate = false;
  modal = {
    modalVisible: false,
    isAdd: true,
  };
  form = {
    id: '',
    name: '',
    status: '1',
  };

  constructor(
    private commodityService: CommodityService,
    private nzMessageService: NzMessageService,
  ) {
  }

  ngOnInit(): void {
    this.queryTableList();
  }

  // 查询分类列表
  queryTableList(): void {
    this.commodityService.httpGet(this.commodityService.serviceUrl.category, null).then((res: any) => {
      this.listOfData.records = [];
      if (res.code === 0) {
        this.listOfData.records = res.data;
      }
    });
  }

  onPageIndexChange(index: number) {
    this.query.current = index;
    this.queryTableList();
  }

  onPageSizeChange(index: number) {
    this.query.current = index;
    this.queryTableList();
  }

  // 显示添加分类模态框
  addCategory() {
    this.modal.modalVisible = true;
    this.modal.isAdd = true;
  }

  // 编辑分类
  edit(data: any) {
    this.form.id = data.id;
    this.form.name = data.name;
    this.form.status = data.status + '';
    this.modal.modalVisible = true;
    this.modal.isAdd = false;
  }

  // 模态框取消按钮监听
  handleModalCancel() {
    this.modal.modalVisible = false;
    this.resetFormData();
  }

  // 模态框确认按钮监听
  handleModalOk() {
    if (!this.form.name) {
      return this.nzMessageService.warning('请输入分类名称');
    }
    this.commodityService.httpPost(
      this.form.id ? this.commodityService.serviceUrl.commodityTypeEdit : this.commodityService.serviceUrl.commodityTypeAdd,
      this.form).then((res: any) => {
      if (res.code !== 0) {
        this.nzMessageService.error(res.message);
      } else {
        this.nzMessageService.success('操作成功');
        this.queryTableList();
      }
    }).finally(() => {
      this.modal.modalVisible = false;
      this.resetFormData();
    });
    return;
  }

  // 重置数据
  resetFormData() {
    this.form = {
      id: '',
      name: '',
      status: '1',
    };
  }

  // 删除分类
  deleteRow(id: any) {
    this.commodityService.httpGet(this.commodityService.serviceUrl.commodityTypeDelete, {id}).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('删除成功');
        this.queryTableList();
      } else {
        this.nzMessageService.error('删除失败');
      }
    });
  }

  // 移动行
  moveRow(id: string, mode: string) {
    let moveType = 0;
    switch (mode) {
      case 'up':
        moveType = 1;
        break;
      case 'dn':
        moveType = 0;
        break;
    }
    this.commodityService.httpPost(this.commodityService.serviceUrl.commodityTypeMove, {id, moveType}).then((res: any) => {
      if (res.code !== 0) {
        this.nzMessageService.error(res.message);
      } else {
        this.queryTableList();
      }
    });
  }

  // 启用禁用分类
  using(item: any) {
    this.commodityService.httpPost(
      this.commodityService.serviceUrl.commodityTypeEdit,
      {...item, status: item.status === 1 ? 0 : 1}).then((res: any) => {
      if (res.code !== 0) {
        this.nzMessageService.error(res.message);
      } else {
        this.nzMessageService.success('操作成功');
        this.queryTableList();
      }
    }).finally(() => {
      this.modal.modalVisible = false;
      this.resetFormData();
    });
  }
}
